package cn.momoshenchi.medicine.controller;

import cn.momoshenchi.medicine.pojo.BaseDoctor;
import cn.momoshenchi.medicine.pojo.ConsultAsk;
import cn.momoshenchi.medicine.service.ConsultAskService;
import cn.momoshenchi.medicine.util.AssertUtil;
import cn.momoshenchi.medicine.util.HttpRequestUtil;
import cn.momoshenchi.medicine.util.PageUtil;
import cn.momoshenchi.medicine.util.ResultInfo;
import cn.momoshenchi.medicine.vo.Consult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @version : 1.0
 * @author: momoshenchi
 * @date: 2021/7/13 - 14:21
 */
@RestController
public class ConsultController
{
    @Autowired
    private ConsultAskService consultAskService;

    /**
     * 用户添加复诊配药
     *
     * @param
     * @return
     */
    @PostMapping("user/consult/file")
    public ResultInfo consult(@Valid Consult consult, MultipartFile[] files) throws IOException, IllegalAccessException, InstantiationException, InvocationTargetException
    {
        AssertUtil.isTrue(files == null, "请先选择图片");
        consultAskService.consult(consult, files);
        return new ResultInfo();
    }

    @PostMapping("user/consult")
    public ResultInfo consult(@Valid Consult consult) throws IOException, IllegalAccessException, InstantiationException, InvocationTargetException
    {
        consultAskService.consultByImg(consult);
        return new ResultInfo();
    }

    @PostMapping("/upload")
    public ResultInfo test(@RequestBody MultipartFile[] files) throws IOException
    {
        AssertUtil.isTrue(files == null, "请先选择图片");
        String imgUrl = consultAskService.upload(files);
        return new ResultInfo(imgUrl);
    }

    /**
     * 分页查询用户复诊配药记录,分页参数可选
     *
     * @param
     * @return
     */
    @PostMapping("/consult/history")
    public ResultInfo history(@RequestBody Map<String, Object>map)
    {
        String userId= (String) map.get("userId");
        Integer page= (Integer) map.get("page");
        Integer size= (Integer) map.get("size");
        IPage<ConsultAsk> consultPage =null;
        if(page!=null&&size!=null)
        {
            consultPage=new Page<>(page, size);
        }
        else
        {
            consultPage=new Page<>(1, 5);
        }
        consultPage= consultAskService.page(consultPage,new QueryWrapper<ConsultAsk>().eq("create_user_id", userId));
        return new ResultInfo(consultPage);
    }

    /**
     * 查询复诊配药详请
     * 注意:为什么这里是一个list,前端的需要
     * @param consultId 请求id
     * @return
     */
    @GetMapping("/consult/{consultId}")
    public ResultInfo detail(@PathVariable String consultId)
    {
        ConsultAsk detail = consultAskService.getById(consultId);
        List<ConsultAsk>list=new ArrayList<>();
        list.add(detail);
        return new ResultInfo(list);
    }

    /**
     * 医生开启接诊
     *
     * @param consultId
     * @return
     */
    @PostMapping("/consult/start/{consultId}")
    public ResultInfo start(@PathVariable String consultId)
    {
        AssertUtil.isTrue(! consultAskService.update(new UpdateWrapper<ConsultAsk>().set("consult_status", Byte.parseByte("2")).set("accept_time", new Date()).eq("consult_id", consultId)),"就诊失败");
        return new ResultInfo();
    }



}
